package com.itheima.youdianda.controller;

import com.itheima.youdianda.service.SysMenuService;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.entity.SysMenu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("sys_menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;


    /**
     * 所有菜单
     * @return
     */
    @GetMapping("/allData")
    public R<List<SysMenu>> allData(){
        List<SysMenu> menus = sysMenuService.list();
        return R.success(menus);
    }


    /**
     * 菜单添加
     * @param sysMenu
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody SysMenu sysMenu){
        log.info("新增菜单的信息为:{}",sysMenu.toString());
        sysMenuService.save(sysMenu);
        return R.success("添加成功");
    }


    /**
     * 菜单删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String> del(@RequestParam Integer id){
        log.info("删除菜单的id为{}",id);
        sysMenuService.removeById(id);
        return R.success("删除成功");
    }


    /**
     *菜单详情,用于修改菜单的数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<SysMenu> rowInfo(@RequestParam Integer id){
        log.info("前端传递来的菜单id为{}",id);
        SysMenu menu = sysMenuService.getById(id);
        return R.success(menu);
    }


    /**
     * 菜单修改
     * @param sysMenu
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody SysMenu sysMenu){
        log.info("菜单数据为:{}",sysMenu.toString());
        sysMenuService.updateById(sysMenu);
        return R.success("修改成功");
    }

}
